﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.IO;
using System.Collections;
using System.Text;
using System.Security.Cryptography;

namespace ImbedLibrary
{
    /// <summary>
    /// 添加serv-u帐号
    /// </summary>
    public class FtpTools
    {
        /// <summary>
        /// 添加serv-u帐号
        /// </summary>
        /// <param name="login">帐号</param>
        /// <param name="pwd">密码</param>
        /// <param name="path">文件地址</param>
        /// <param name="webPath">网站物理地址</param>
        /// <returns>信息</returns>
        public static void AddServUser(string login, string pwd, string path, string webPath)
        {
            try
            {
                //读取文件 开始
                StreamReader sr = new StreamReader(path, Encoding.GetEncoding("gb2312"));
                string sLine = "";
                ArrayList arrText = new ArrayList();
                while (sLine != null)
                {
                    sLine = sr.ReadLine();
                    if (sLine != null)
                        arrText.Add(sLine);
                }
                sr.Close();
                //读取文件 结束

                for (int i = 0; i < arrText.Count; i++)
                {
                    if (arrText[i].ToString().Contains("[USER"))
                    {
                        arrText.Insert(i, "User" + (i - 5) + "=" + login + "|1|0");
                        break;
                    }
                }

                string content = "";
                for (int i = 0; i < arrText.Count; i++)
                {
                    content += arrText[i] + "\r\n";
                }

                string strmd5 = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile("lh" + pwd, "MD5");  //把两位随机字母和md5连接并再次进行MD5加密
                string PwdOk = "lh" + strmd5;

                //添加ser-u 属性开始

                content += "[USER=" + login + "|1]\r\n";

                content += "Password=" + PwdOk;
                content += "\r\n" + @"HomeDir=" + webPath + "\r\n";
                content += "RelPaths=1\r\n";
                content += "DiskQuota=1|524288000|0\r\n";
                content += "PasswordLastChange=1309395953\r\n";
                content += "TimeOut=600\r\n";
                content += @"Access1=" + webPath + "|RWAMLCDP\r\n";

                //添加ser-u 属性结束

                //写出文件 开始
                FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write);
                StreamWriter m_streamWriter = new StreamWriter(fs, Encoding.GetEncoding("gb2312"));
                m_streamWriter.Flush();
                // 使用StreamWriter来往文件中写入内容
                m_streamWriter.BaseStream.Seek(0, SeekOrigin.Begin);
                // 把richTextBox1中的内容写入文件
                m_streamWriter.Write(content);
                //关闭此文件
                m_streamWriter.Flush();
                m_streamWriter.Close();
                //写出文件 结束
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}
